<?php
include_once 'des.class.php';
/**
 * 缓存操作类
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0 GetUser add by yaoli<yaoli@pateo.com.cn> 
 */
class cache extends ActionBase1 {
	
	private $date = null;
	public $isdes = 0;
	
	function init() {
		
	}
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	public function action() {
	
	}
	
	public function clearSmartyCache() {
		$smarty = load_libra ( 'smarty' );
		// cho $smarty->cache_dir;
		$compiledir = $smarty->compile_dir;
		
		// 使用dir函数，返回$compiledir路径的对象directory
		$directory = dir ( $compiledir );
		
		while ( $entry = $directory->read () ) {
			$filename = $compiledir . '/' . $entry;
			if (is_file ( $filename )) {
				if (@unlink ( $filename )) {
					echo "成功删除：" . $filename . "<br>";
				}
			}
		}
		$directory->close ();
	}
	
	public function clearSmartyCache1() {
		$smarty = load_libra ( 'smarty' );
		// cho $smarty->cache_dir;
		$compiledir = $smarty->compile_dir;
		
		// 使用dir函数，返回$compiledir路径的对象directory
		$directory = opendir ( $compiledir );
		
		while ( $entry = readdir ( $directory ) ) {
			$filename = $compiledir . '/' . $entry;
			if (is_file ( $filename )) {
				if (@unlink ( $filename )) {
					echo "成功删除：" . $filename . "<br>";
				}
			}
		}
		closedir ( $directory );
	}
	
	/**
	 *
	 * @param $name unknown_type
	 *       	 缓存名称
	 * @param $data unknown_type
	 *       	 缓存数据
	 * @param $savetype unknown_type
	 *       	 缓存类型
	 */
	public static function savedata($name, $data, $savetype, $validity = 3600) {
		
		$name1 = $name;
		// 存变量名叫后缀
		$name .= "_" . CACHE_POSTFIX;
		
		if ($savetype == 'session') {
			session_start ();
			ini_set ( 'session.gc_maxlifetime', $validity );
			$_SESSION [$name] = $data;
			session_regenerate_id ();
		} elseif ($savetype == 'cookie') {
			
			
		/* 	// 果数据是数组，序列化
			if (is_array ( $data ) || is_object ( $data )) { */
				
			#	$data = serialize ( $data );
				
		#	}
			/* if($this->isdes){
				$data = serialize ( $data );
				$data = DES::Encrypt($data);
			} */
			
			#$data = DES::Encrypt($data);
			
			// 置cookie名称，值，有效期
			setcookie ( $name, $data, time () + $validity,'/',$_SERVER['SERVER_NAME'] );
			
		
		} elseif ($savetype == 'memcache') {
		
		}
		
		return true;
			
	
	}
	
	/**
	 *
	 * @param $name unknown_type
	 *       	 缓存名称
	 * @param $savetype unknown_type
	 *       	 缓存类型
	 */
	public static function getdata($name, $savetype) {
		// 存变量名叫后缀
		$name .= "_" . CACHE_POSTFIX;
		
		$result = null;
		
		if ($savetype == 'session') {
			session_start ();
			$result = $_SESSION [$name];
		} elseif ($savetype == 'cookie') {
			
			if(isset($_COOKIE[$name])){
				$result = $_COOKIE [$name];
			}
			if($result){
					/* $result = DES::Decrypt($result);			
					$result = unserialize ( $result ); */
				
			}
		
		} elseif ($savetype == 'memcache') {
		
		}
		return $result;
	
	}
	
	public static function deletedata($name,$savetype){
	
		$name1 = $name;
		// 存变量名叫后缀
		$name .= "_" . CACHE_POSTFIX;
		
		if ($savetype == 'session') {
			session_start ();
			$_SESSION [$name] = '';
		} elseif ($savetype == 'cookie') {
			#setcookie ( $name, '', time () -3600,'/',$_SERVER['SERVER_NAME'] );
			setcookie ( $name, '', time () -3600,'/',$_SERVER['SERVER_NAME'] );
		} elseif ($savetype == 'memcache') {
		
		}
		
		if (!self::getdata ( $name1, $savetype )) {
			return true;
		}else{
			return false;
		}
	}
	
	public function readjson() {
		$dir = "C:/Users/dell/Desktop/rbupdate_roadbook2013090209.json";
		$json = file_get_contents ( $dir );
		dump ( json_decode ( $json ) );
	}

}